<extend name="Include:Layout"/>
<block name="title">服务</block>
<block name="style">
<style>
.picker-button .iconfont{display:inline-block;width:30px;font-size:24px;}
#data-content{background:#fff;}
.pengyou{border-bottom:1px solid #ddd;}
.pengyou .cnt{padding:15px;font-size:14px;}
.avater-py img {width:100%;}
.pengyou .cnt .detail{}
.pengyou .cnt .name a{color:#6499b3;}
.pengyou .cnt p{line-height:18px;padding-bottom:5px;} 
.imglist img{width:100%;}
.tools{margin-top:10px;}
.time{font-size:12px;color:#999;}
.red {color:red}
.btn-dianzai i,.btn-dashang i {margin-right:10px;}

.toolbar .left-button {
    position: absolute;
    left: 0;
    box-sizing: border-box;
    height: 2.2rem;
    line-height: 2.2rem;
    color: #04BE02;
    z-index: 1;
    padding: 0 .5rem;
}
</style>
</block>

<block name="main">
	<div class="weui_tab">
		<!-- <div class="weui_navbar" style="display:none;">
			<div class="toolbar">
		        <div class="toolbar-inner">
		            <if condition="$user['role'] eq 'teacher'"><a href="javascript:;" class="picker-button open-popup" data-target="#adddata"><i class="icon iconfont"></i></a></if>
		            <a href="{:U('index')}" class="left-button open-popup">返回</a>
		            <h1 class="title" id="banji_title">教师天地</h1>
		        </div>
		    </div>
		</div> -->
		<div class="weui_tab_bd" id="content" style="margin-top:0px;padding-bottom:55px;background:#fff;">
			<div class="weui_panel weui_panel_access">
			  <div class="weui_panel_bd">
			    <a href="javascript:void(0);" class="weui_media_box weui_media_appmsg">
			      <div class="weui_media_hd">
			        <php>$info['teacher']['avater']?$headimg=$info['teacher']['avater']:$headimg=$info['teacher']['headimgurl'];</php>
			        <img class="weui_media_appmsg_thumb" src="{$headimg|default='/Public/Static/Img/txlogo.jpg'}" alt="">
			      </div>
			      <div class="weui_media_bd">
			        <h4 class="weui_media_title">{$info.teacher.english}</h4>
			        <p class="weui_media_desc">{$info.kecheng}</p>
			      </div>
			    </a>
			  </div>
			</div>
			<div id="data-content" data-page="1">

			</div>
			<div class="weui-infinite-scroll" id='infinite'>
				<div class="infinite-preloader"></div>
				正在加载...
			</div>
		</div>
		<php>$curbar = 3;</php>
    	<include file="Include:navbar" />
    	<input type="hidden" name="uid" value="{$_SESSION['_loginUser_home']['uid']}"/>
    </div>

<div id="adddata" class="weui-popup-container">
  	<div class="weui-popup-overlay"></div>
  	<div class="weui-popup-modal">
  		<div class="toolbar">
	        <div class="toolbar-inner">
	            <a href="javascript:;" class="picker-button close-popup">返回</a>
	            <h1 class="title" id="banji_title">发布状态</h1>
	        </div>
	    </div>
  		<form style="margin-top:44px;" action="{:U('test_submit')}" method="post">
  			<div class="weui_cell">
	            <div class="weui_cell_bd weui_cell_primary">
	                <textarea class="weui_textarea" placeholder="请输入..." rows="3" name="content"></textarea>
	                <div class="weui_textarea_counter"><span>0</span>/200</div>
	            </div>
	        </div>
		    <div class="weui_cells weui_cells_form" style="margin-top:10px;">
		        <div class="weui_cell">
		            <div class="weui_cell_bd weui_cell_primary">
		                <div class="weui_uploader">
		                    <div class="weui_uploader_hd weui_cell">
		                        <div class="weui_cell_bd weui_cell_primary">图片上传</div>
		                        <div class="weui_cell_ft" id="hasupload"></div>
		                    </div>
		                    <div class="weui_uploader_bd">
		                        <ul class="weui_uploader_files" id="imgviews">
		                        </ul>
		                        <div class="weui_uploader_input_wrp">
		                            <input class="weui_uploader_input" type="button" id="chose_imgs" accept="image/jpg,image/jpeg,image/png,image/gif" multiple="">
		                        </div>
		                    </div>
		                </div>
		            </div>
		        </div>
		    </div>
		    <div style="padding:15px;margin-top:30px;"><a href="javascript:;" id="submit" class="weui_btn weui_btn_primary">发布</a></div>
  		</form>
  	</div>
</div>
<script type="text/html" id="data-tpl">
<div class="pengyou">
	<div class="cnt">
		<div class="weui-row">
	      	<div class="weui-col-15 avater-py">{{if headimgurl}}<img src="{{headimgurl}}">{{else}}<img src="http://shp.qpic.cn/weixinsrc_pic/pScBR7sbqjOBJomcuvVJ6iacVrbMJaoJZkFUIq4nzQZUIqzTKziam7ibg/">{{/if}}</div>
	      	<div class="weui-col-85 detail">
	      		<p class="name"><a href="javascript:;">{{#realname}}老师</a></p>
	      		<p class="text">{{desc}}</p>
				{{if imglist}}
	      		<div class="imglist">
	      			<div class="weui-row" >
						{{each imglist as value index}}
				      	<div class="weui-col-33"><img src="{{value.url}}"></div>
				      	{{/each}}
				    </div>
	      		</div>
				{{/if}}
	      		<p class="time">发布于 {{create_time}}</p>
	      		<div class="tools">
	      			<div class="weui-row">
				      	<div class="weui-col-50 ">
				      		<button class="weui_btn weui_btn_mini weui_btn_default btn-dashang" data-id="{{id}}"><i class="icon iconfont {{if dashang}}red{{/if}}"></i><span>{{if dashang}}已打赏{{else}}打赏{{/if}}</span></button>
				      	</div>
				      	<div class="weui-col-50">
				      		<a href="javascript:;" class="weui_btn weui_btn_mini weui_btn_default btn-dianzai" data-id="{{id}}" onclick="dianzai({{id}});"><i class="icon iconfont  {{if dianzai}}red{{/if}}"></i><span>{{if dianzai}}取消{{else}}点赞{{/if}}</span></a>
				      	</div>
				     </div>
	      		</div>
	      	</div>
	    </div>
	</div>
</div>
</script>
</block>

<block name="script">
<js href="/Public/Assets/jqweui/dist/js/swiper.js" />
<js href="http://res.wx.qq.com/open/js/jweixin-1.0.0.js" />
<js href="/weixin/js/get_js_ticket" />
<script>
wx.config({
    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
    appId: wxconfig.appId, // 必填，公众号的唯一标识
    timestamp: wxconfig.timestamp, // 必填，生成签名的时间戳
    nonceStr: wxconfig.nonceStr, // 必填，生成签名的随机串
    signature: wxconfig.signature,// 必填，签名，见附录1
    jsApiList: [
                'checkJsApi',
                'chooseImage',
                'previewImage',
                'uploadImage',
                'downloadImage',

              ] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
});
wx.ready(function(){
	
	// 5.1 拍照、本地选图
    var images = {
      localId: [],
      serverId: []
    };
	//5.3 上传图片
	$('#chose_imgs').click(function(){
      var that =$(this);
      images.localId = [];
      wx.chooseImage({
        success: function (res) {
          images.localId = res.localIds;
          if (images.localId.length == 0) {
              alert('请先使用 chooseImage 接口选择图片');
              return;
          }
		  /*
          if(images.localId.length > 1) {
              alert('目前仅支持单张图片上传,请重新上传');
              images.localId = [];
              return;
          }*/
          var i = 0, length = images.localId.length;
          images.serverId = [];
          
          function upload() {
	             wx.uploadImage({
	                        localId: images.localId[i],
	                        success: function (res) {
	                        		   
                            	var html = '<li class="weui_uploader_file candel" data-mediaID="'+res.serverId+'" style="border:1px solid #eee;background-image:url('+images.localId[i]+')"><input type="hidden" name="media_id[]" value="'+res.serverId+'"></li>';
                            	$('#imgviews').append(html);
                            	i++; 
                            	$('#hasupload').html('已上传：' + i + '/' + length);                                       
	                            images.serverId.push(res.serverId);
	                            //alert(JSON.stringify(images.serverId));
	                            //download(res.serverId);
	                            //get_img_url(res.serverId);
	                            listen_del_img();
	                            
	                            if (i < length) {
	                            	
	                                upload();
	                            }

	                        },
	                        fail: function (res) {
	                             alert(JSON.stringify(res));
	                         }
	              });
          }
          upload();
          /*
          function download(sid){
        	  if(!sid) return ;
        	  wx.downloadImage({
          	    serverId: sid, // 需要下载的图片的服务器端ID，由uploadImage接口获得
          	    isShowProgressTips: 1, // 默认为1，显示进度提示
          	    success: function (res) {
          	        var localId = res.localId; // 返回图片下载后的本地ID
          	        alert(localId);
          	    }
          	});
          }
          
          function get_img_url(mid){
        	  if(!mid) return ;
        	  $.get('/weixin/js/get_image_tmpurl/media_id/'+mid,function(ret){
        		 if(ret.status){
        			 alert(ret.data);
        		 }else{
        			 alert('获取失败');
        		 }
        	  });
          }
          */
        }
      });
	});
	
	//现在是测试数据提交
	$("#submit").click(function(){
	    $.ajax({
            type: 'post',
            url: $("form").attr('action'),
            data : $("form").serialize(),
            dataType: 'json',
            success : function(data){
                    if(data.status){
                    	$.closePopup()
                    	window.location.reload();      
                        //alert(data.info);
                       
                            
                    }else{  

                    	$.toast(data.info, "text");
                    }
                    
                                            
            }
        });
	    //return false;
	 
	    /*$("form").submit();*/
	}) 
	
});
function listen_del_img(){
	
	$('.candel').click(function(){
		if(confirm("删除该图片吗？")){
			$(this).remove();
		}	
	});
}
/** 
*删除数组指定下标或指定对象 
*/ 
Array.prototype.remove=function(obj){ 
	for(var i =0;i <this.length;i++){ 
		var temp = this[i]; 
		if(!isNaN(obj)){ 
		temp=i; 
	} 
	if(temp == obj){ 
		for(var j = i;j <this.length;j++){ 
		this[j]=this[j+1]; 
	} 
	this.length = this.length-1; 
	} 
	} 
} 
$(document.body).infinite();
var loading = false;  //状态标记
$("#content").infinite().on("infinite", function() {
  if(loading) return;
  loading = true;
  load();
});
$(document).on('click','.btn-dianzai',function(){
	$.showLoading();
	var that = $(this);
	var id = that.attr("data-id");
	var uid = $("input[name=uid]").val();
	$.post('/api/ygcl/dianzai',{id:id,type:'teacher',uid:uid},function(ret){
		//console.log(ret);
		$.hideLoading();
		if(ret == '取消'){
			that.find("i").addClass("red");
		}else{
			that.find("i").removeClass("red");
		}
		that.find("span").html(ret);
	})
})
template.helper('decodehtmljson', function (str) {
	str = str.replace(new RegExp(/(\/\/&quot;)/g),'');
	str = str.replace(/&amp;/gi, '&');
	str = str.replace(/&nbsp;/gi, ' ');
	str = str.replace(/&quot;/gi, '"');
	str = str.replace(/&#39;/g, "'");
	str = str.replace(/&lt;/gi, '<');
	str = str.replace(/&gt;/gi, '>');
	//str = str.replace(/<br[^>]*>(?:(rn)|r|n)?/gi, '');
	return str;
});

$(function () {
	//第一次加载数据
	load();
});
function load(){
	 var page=$("#data-content").attr("data-page");
	 var uid = {$_GET['uid']};
	 setTimeout(function() {
		 $.get('/home/fuwu/getjiaoshitiandi',{uid:uid,page:page},function(ret){
				var html = '' ;
				var tmp = ret.info;
				for(var i = 0 ; i <tmp.length ; i++){
					html += template('data-tpl',tmp[i]);
				}
				var p = parseInt(page)+1;
				$('#data-content').append(html).attr("data-page",p);
				listen_img_click(); //图片点击事件监听
				//$.hideLoading();
				//$("#content").pullToRefreshDone();
				loading = false;
				if(ret.next == 'no'){
					$("#content").destroyInfinite();
					$("#infinite").hide();
				}
			}); 
	 },1000);
}
function listen_img_click(){
	$('.imglist img').click(function(){
		var $parent = $(this).parent().parent();
		var index = $parent.find('img').index($(this)); //获取当前点击的是哪一张
		var $imglist = $parent.find('img');
		var items = [];
		$imglist.each(function(){
			items.push($(this).attr('src'));
		});
		
		var pb = $.photoBrowser({
			initIndex:index,  //从第几张开始
			items: items
		});
		pb.open();
	});
}
$(document).on('click','.btn-dashang',function(){
	var thisBtn = $(this);
	var tcid = $(this).attr("data-id");//教师内容id
	var uid = $("input[name=uid]").val();//用户id
	var did = {$_GET['did']};//校区id
	thisBtn.attr('disabled', 'disabled');
	var data = {
			uid:uid,
			tcid:tcid,
			did:did
		};
	$.showLoading();
	$.post('/api/ygcl/savedashang',data,function(data){
		$.hideLoading();
		thisBtn.removeAttr('disabled');
		if(data.status){
			if(data.data){
				location.href="/weixin/pay/index/id/"+data.data;
			}else{
				$.alert(data.data,'');
			}
		}
	})
})
</script>
</block>